package com.lehecai.core.service.impl.redis;

import com.lehecai.core.service.redis.RedisManager;
import redis.clients.jedis.JedisCommands;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * User: leiming
 */
public class ShardedJedisPoolRedisManagerImpl implements RedisManager {

    private ShardedJedisPool shardedJedisPool;

    @Override
    public JedisCommands getConnection() {
        try{
            return shardedJedisPool.getResource();
        }catch (JedisConnectionException jce){
            return null;
        }
    }

    @Override
    public void releaseConnection(JedisCommands jedisCommands) {
        if(jedisCommands != null){
            shardedJedisPool.returnResource((ShardedJedis)jedisCommands);
        }
    }

    @Override
    public void destroy() {
        if (shardedJedisPool != null) {
            shardedJedisPool.destroy();
        }
    }

    public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
        this.shardedJedisPool = shardedJedisPool;
    }
}
